<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>EulerianPath.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="EulerianPath code in Java">
<meta NAME="TITLE" CONTENT="EulerianPath code in Java">
<meta NAME="KEYWORDS" CONTENT="EulerianPath,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>EulerianPath.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "EulerianPath.java">EulerianPath.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac EulerianPath.java</span>
<span class="comment"> *  Execution:    java EulerianPath V E</span>
<span class="comment"> *  Dependencies: Graph.java Stack.java StdOut.java</span>
<span class="comment"> *</span>
<span class="comment"> *  Find an Eulerian path in a graph, if one exists.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">Iterator</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">EulerianPath</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type</span>
<span class="comment"> *  for finding an Eulerian path in a graph.</span>
<span class="comment"> *  An </span><span class="keyword">&lt;em&gt;</span><span class="comment">Eulerian path</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is a path (not necessarily simple) that</span>
<span class="comment"> *  uses every edge in the graph exactly once.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses a nonrecursive depth-first search.</span>
<span class="comment"> *  The constructor runs in O(</span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) time,</span>
<span class="comment"> *  and uses O(</span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) extra space,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of edges and </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> the number of vertices</span>
<span class="comment"> *  All other methods take O(1) time.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  To compute Eulerian cycles in graphs, see {</span><span class="type">@link</span><span class="comment"> EulerianCycle}.</span>
<span class="comment"> *  To compute Eulerian cycles and paths in digraphs, see</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> DirectedEulerianCycle} and {</span><span class="type">@link</span><span class="comment"> DirectedEulerianPath}.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/41graph"</span><span class="keyword">&gt;</span><span class="comment">Section 4.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> * </span>
<span class="comment"> * </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> * </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> * </span><span class="type">@author</span><span class="comment"> Nate Liu</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">EulerianPath</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Stack&lt;Integer&gt;</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span><span class="normal">   </span><span class="comment">// Eulerian path; null if no suh path</span>

<span class="normal">    </span><span class="comment">// an undirected edge, with a field to indicate whether the edge has already been used</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Edge</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> isUsed</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w </span><span class="symbol">=</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">            isUsed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// returns the other vertex of the edge</span>
<span class="normal">        </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">other</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> vertex</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Illegal endpoint"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes an Eulerian path in the specified graph, if one exists.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the graph</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">EulerianPath</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// find vertex from which to start potential Eulerian path:</span>
<span class="normal">        </span><span class="comment">// a vertex v with odd degree(v) if it exits;</span>
<span class="normal">        </span><span class="comment">// otherwise a vertex with degree(v) &gt; 0</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> oddDegreeVertices </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="function">nonIsolatedVertex</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">degree</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">%</span><span class="normal"> </span><span class="number">2</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                oddDegreeVertices</span><span class="symbol">++;</span>
<span class="normal">                s </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// graph can't have an Eulerian path</span>
<span class="normal">        </span><span class="comment">// (this condition is needed for correctness)</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">oddDegreeVertices </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// special case for graph with zero edges (has a degenerate Eulerian path)</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">s </span><span class="symbol">==</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">)</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// create local view of adjacency lists, to iterate one vertex at a time</span>
<span class="normal">        </span><span class="comment">// the helper Edge data type is used to avoid exploring both copies of an edge v-w</span>
<span class="normal">        Queue</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;[]</span><span class="normal"> adj </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Queue</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;[])</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            adj</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Edge</span><span class="symbol">&gt;();</span>

<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> selfLoops </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="comment">// careful with self loops</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">==</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">selfLoops </span><span class="symbol">%</span><span class="normal"> </span><span class="number">2</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                        </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                        adj</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                        adj</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">].</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="cbracket">}</span>
<span class="normal">                    selfLoops</span><span class="symbol">++;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="usertype">Edge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Edge</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">,</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                    adj</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                    adj</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">].</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// initialize stack with any non-isolated vertex</span>
<span class="normal">        </span><span class="usertype">Stack&lt;Integer&gt;</span><span class="normal"> stack </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        stack</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// greedily search through edges in iterative DFS style</span>
<span class="normal">        path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">stack</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> stack</span><span class="symbol">.</span><span class="function">pop</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">adj</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">Edge</span><span class="normal"> edge </span><span class="symbol">=</span><span class="normal"> adj</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">].</span><span class="function">dequeue</span><span class="symbol">();</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">edge</span><span class="symbol">.</span><span class="normal">isUsed</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">                edge</span><span class="symbol">.</span><span class="normal">isUsed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">                stack</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">                v </span><span class="symbol">=</span><span class="normal"> edge</span><span class="symbol">.</span><span class="function">other</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="comment">// push vertex with no more leaving edges to path</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check if all edges are used</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">path</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span>
<span class="normal">            path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="function">certifySolution</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the sequence of vertices on an Eulerian path.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the sequence of vertices on an Eulerian path;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Integer&gt;</span><span class="normal"> </span><span class="function">path</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> path</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the graph has an Eulerian path.</span>
<span class="comment">     * </span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the graph has an Eulerian path;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasEulerianPath</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> path </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// returns any non-isolated vertex; -1 if no such vertex</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">nonIsolatedVertex</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">degree</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**************************************************************************</span>
<span class="comment">     *</span>
<span class="comment">     *  The code below is solely for testing correctness of the data type.</span>
<span class="comment">     *</span>
<span class="comment">     **************************************************************************/</span>

<span class="normal">    </span><span class="comment">// Determines whether a graph has an Eulerian path using necessary</span>
<span class="normal">    </span><span class="comment">// and sufficient conditions (without computing the path itself):</span>
<span class="normal">    </span><span class="comment">//    - degree(v) is even for every vertex, except for possibly two</span>
<span class="normal">    </span><span class="comment">//    - the graph is connected (ignoring isolated vertices)</span>
<span class="normal">    </span><span class="comment">// This method is solely for unit testing.</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasEulerianPath</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// Condition 1: degree(v) is even except for possibly two</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> oddDegreeVertices </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">degree</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">%</span><span class="normal"> </span><span class="number">2</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span>
<span class="normal">                oddDegreeVertices</span><span class="symbol">++;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">oddDegreeVertices </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// Condition 2: graph is connected, ignoring isolated vertices</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> </span><span class="function">nonIsolatedVertex</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">BreadthFirstPaths</span><span class="normal"> bfs </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BreadthFirstPaths</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">.</span><span class="function">degree</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">bfs</span><span class="symbol">.</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// check that solution is correct</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">certifySolution</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// internal consistency check</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasEulerianPath</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="symbol">(</span><span class="function">path</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// hashEulerianPath() returns correct value</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasEulerianPath</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="function">hasEulerianPath</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// nothing else to check if no Eulerian path</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">path </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// check that path() uses correct number of edges</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">path</span><span class="symbol">.</span><span class="function">size</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">E</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// check that path() is a path in G</span>
<span class="normal">        </span><span class="comment">// TODO</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">unitTest</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> description</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">description</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"-------------------------------------"</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>

<span class="normal">        </span><span class="usertype">EulerianPath</span><span class="normal"> euler </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">EulerianPath</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">);</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"Eulerian path:  "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">euler</span><span class="symbol">.</span><span class="function">hasEulerianPath</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">:</span><span class="normal"> euler</span><span class="symbol">.</span><span class="function">path</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"none"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">EulerianPath</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> V </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> E </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>


<span class="normal">        </span><span class="comment">// Eulerian cycle</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G1 </span><span class="symbol">=</span><span class="normal"> GraphGenerator</span><span class="symbol">.</span><span class="function">eulerianCycle</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G1</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"Eulerian cycle"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// Eulerian path</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G2 </span><span class="symbol">=</span><span class="normal"> GraphGenerator</span><span class="symbol">.</span><span class="function">eulerianPath</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G2</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"Eulerian path"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// add one random edge</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G3 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Graph</span><span class="symbol">(</span><span class="normal">G2</span><span class="symbol">);</span>
<span class="normal">        G3</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">),</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G3</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"one random edge added to Eulerian path"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// self loop</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G4 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Graph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> v4 </span><span class="symbol">=</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        G4</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">v4</span><span class="symbol">,</span><span class="normal"> v4</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G4</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"single self loop"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// single edge</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G5 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Graph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        G5</span><span class="symbol">.</span><span class="function">addEdge</span><span class="symbol">(</span><span class="normal">StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">),</span><span class="normal"> StdRandom</span><span class="symbol">.</span><span class="function">uniform</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G5</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"single edge"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// empty graph</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G6 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Graph</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G6</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"empty graph"</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// random graph</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G7 </span><span class="symbol">=</span><span class="normal"> GraphGenerator</span><span class="symbol">.</span><span class="function">simple</span><span class="symbol">(</span><span class="normal">V</span><span class="symbol">,</span><span class="normal"> E</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">unitTest</span><span class="symbol">(</span><span class="normal">G7</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"simple graph"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
